import java.awt.BorderLayout;
import java.awt.Cursor;
import java.awt.EventQueue;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.Color;
import java.awt.Font;
import java.util.Observable;
import java.util.Observer;


public class PlayerSelectionUI extends JFrame implements Observer {
	
	private JPanel contentPane, panel;
	private JLabel playerMoneyLbl;
	private ImageIcon 	dice, diceOver, trade, tradeOver,
						property, propertyOver, quit, quitOver, bail, bailOver;
	private int tempCurrentPlayerMoney;
	
	Player p;

	public PlayerSelectionUI(Player play) {

		setTitle("Player Selection Screen");
		
		this.p=play;
		this.tempCurrentPlayerMoney = this.p.getCurrentMoney();
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		
		setBounds(102, 170, 600, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		contentPane.setName("Test");
		this.setVisible(true);
		this.setSize(600,300);
	    this.setResizable(false);
		
		setContentPane(contentPane);
		
		this.dice = new ImageIcon("Graphics/dice.png");
		this.diceOver = new ImageIcon("Graphics/diceOver.png");
		
		this.quit = new ImageIcon("Graphics/quit.png");
		this.quitOver = new ImageIcon("Graphics/quitOver.png");
		
		this.property = new ImageIcon("Graphics/property.png");
		this.propertyOver = new ImageIcon("Graphics/propertyOver.png");
		
		this.trade = new ImageIcon("Graphics/trade.png");
		this.tradeOver = new ImageIcon("Graphics/tradeOver.png");
		
		this.bail = new ImageIcon("Graphics/bail.png");
		this.bailOver = new ImageIcon("Graphics/bailover.png");
		
		
		panel = new JPanel();
		panel.setBackground(Color.WHITE);
		contentPane.add(panel, BorderLayout.CENTER);
		panel.setLayout(null);
		
		JButton dicebtn = new JButton(dice);
		dicebtn.setForeground(Color.BLACK);
		dicebtn.setBackground(Color.BLACK);
		dicebtn.setCursor(new Cursor(Cursor.HAND_CURSOR));
		dicebtn.setBorderPainted(false);
		dicebtn.setBounds(25, 120, 111, 91);
		dicebtn.setRolloverEnabled(true);
		dicebtn.setRolloverIcon(diceOver);
		dicebtn.setContentAreaFilled(false);
        dicebtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                diceSelect(evt);
            }
        });
		panel.add(dicebtn);
		
		JButton quitbtn = new JButton(quit);
		quitbtn.setCursor(new Cursor(Cursor.HAND_CURSOR));
		quitbtn.setForeground(Color.WHITE);
		quitbtn.setBackground(Color.WHITE);
		quitbtn.setBorderPainted(false);
		quitbtn.setBounds(450, 120, 111, 91);
		quitbtn.setRolloverEnabled(true);
		quitbtn.setRolloverIcon(quitOver);
        quitbtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                quitSelect(evt);
            }
        });
		panel.add(quitbtn);
		
		JButton tradebtn = new JButton(trade);
		tradebtn.setCursor(new Cursor(Cursor.HAND_CURSOR));
		tradebtn.setForeground(Color.WHITE);
		tradebtn.setBackground(Color.WHITE);
		tradebtn.setBorderPainted(false);
		tradebtn.setBounds(305, 120, 111, 91);
		tradebtn.setRolloverEnabled(true);
		tradebtn.setRolloverIcon(tradeOver);
        tradebtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tradeSelect(evt);
            }
        });
		panel.add(tradebtn);
		
		JButton propertybtn = new JButton(property);
		propertybtn.setCursor(new Cursor(Cursor.HAND_CURSOR));
		propertybtn.setForeground(Color.WHITE);
		propertybtn.setBackground(Color.WHITE);
		propertybtn.setBorderPainted(false);
		propertybtn.setBounds(165, 120, 111, 91);
		propertybtn.setRolloverEnabled(true);
		propertybtn.setRolloverIcon(propertyOver);
        propertybtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                propertySelect(evt);
            }
        });
		panel.add(propertybtn);
		
		JButton bailbtn;
		if(p.getCurrentBlockPos().getBlockNumber() == 40){

			bailbtn = new JButton(bail);
			bailbtn.setForeground(Color.WHITE);
			bailbtn.setBackground(Color.WHITE);
			bailbtn.setBorderPainted(false);
			bailbtn.setBounds(450, 15, 111, 91);
			bailbtn.setRolloverEnabled(true);
			bailbtn.setRolloverIcon(bailOver);
			bailbtn.addActionListener(new java.awt.event.ActionListener() {
	        public void actionPerformed(java.awt.event.ActionEvent evt) {
	        			bailSelect(evt);
		            }
		        });
			panel.add(bailbtn);
			new Sound().play("Sounds/jail.wav");
			
			//disable button if money is less than 50
			if(p.getCurrentMoney()<50){
				
				bailbtn.setEnabled(false);
				bailbtn.setToolTipText("Hindi ka pwedeng mag pyansa dahil kulang ang pera mo");
				
			}

		}
		
		
		JLabel label = new JLabel("Roll Dice");
		label.setBounds(51, 222, 69, 14);
		panel.add(label);
		
		JLabel label_1 = new JLabel("Property");
		label_1.setBounds(200, 222, 59, 14);
		panel.add(label_1);
		
		JLabel label_2 = new JLabel("Trade");
		label_2.setBounds(342, 222, 59, 14);
		panel.add(label_2);
		
		JLabel label_3 = new JLabel("Quit Game");
		label_3.setBounds(477, 222, 67, 14);
		panel.add(label_3);
		
		JLabel playerNameLabel = new JLabel("Player Name");
		playerNameLabel.setFont(new Font("Stencil", Font.ITALIC, 22));
		playerNameLabel.setBounds(51, 10, 161, 22);
		panel.add(playerNameLabel);
		
		JLabel playerMoneyLabel = new JLabel("Total Money");
		playerMoneyLabel.setFont(new Font("Stencil", Font.ITALIC, 16));
		playerMoneyLabel.setBounds(94, 38, 110, 22);
		panel.add(playerMoneyLabel);
		
		JLabel playerJailLabel = new JLabel("Get-Out-Of-Jail Tokens");
		playerJailLabel.setFont(new Font("Stencil", Font.ITALIC, 16));
		playerJailLabel.setBounds(10, 65, 262, 22);
		panel.add(playerJailLabel);
		
		JLabel label_7 = new JLabel(this.p.getPlayerName());
		label_7.setFont(new Font("Stencil", Font.ITALIC, 17));
		label_7.setBounds(220, 11, 126, 22);
		panel.add(label_7);
		
		this.playerMoneyLbl = new JLabel(Integer.toString(this.tempCurrentPlayerMoney));
		this.playerMoneyLbl.setFont(new Font("Stencil", Font.ITALIC, 13));
		this.playerMoneyLbl.setBounds(220, 38, 126, 22);
		panel.add(this.playerMoneyLbl);
		
		JLabel label_9 = new JLabel(Integer.toString(this.p.getChanceJailToken()+this.p.getCommunityJailToken()));
		label_9.setFont(new Font("Stencil", Font.ITALIC, 13));
		label_9.setBounds(220, 65, 126, 22);
		panel.add(label_9);
			
	}
	
   private void diceSelect(java.awt.event.ActionEvent evt) {
        CommandWithInt command = new PlayerSelectionCommand();
        command.execute(1);
        this.dispose();
    }

   private void propertySelect(java.awt.event.ActionEvent evt) {
       CommandWithInt command = new PlayerSelectionCommand();
       command.execute(2);
       
   }
   
   private void tradeSelect(java.awt.event.ActionEvent evt) {
	   CommandWithInt command = new PlayerSelectionCommand();
       command.execute(3);

   }
   
   private void quitSelect(java.awt.event.ActionEvent evt) {
       CommandWithInt command = new PlayerSelectionCommand();
       command.execute(4);
       this.dispose();
   }
   
   private void bailSelect(java.awt.event.ActionEvent evt) {
       PlayerSelectionCommand playSelectComm = new PlayerSelectionCommand();
       playSelectComm.execute(5);
       this.dispose();
   }
   
   public void update(Observable obj, Object arg){
	   tempCurrentPlayerMoney=(Integer) arg;

	   this.playerMoneyLbl.setVisible(false);
	   panel.remove(this.playerMoneyLbl);
	   this.playerMoneyLbl = new JLabel(Integer.toString(this.tempCurrentPlayerMoney));
	   this.playerMoneyLbl.setFont(new Font("Stencil", Font.ITALIC, 13));
	   this.playerMoneyLbl.setBounds(220, 38, 126, 22);
	   panel.add(this.playerMoneyLbl);
   }
   
}
